package testssss;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * Yao
 * 进阶测试
 */
public class AvtivitiBusinessDemo {
    /**
     * 添加业务Key 到activiti 表中
     */
    @Test
    public void testAddBusinessKey() {

        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.启动流程实例，同时添加业务key
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("_100", "1001");
//        //4.获取流程实例的id
//        String processInstanceId = pi.getId();
        //5.根据流程实例id，查询流程实例对象，并获取业务key
        String businessKey = pi.getBusinessKey();
        System.out.println(businessKey);
    }
    /**
     * 全部流程示例挂起 /激活
     */
    @Test
    public void testSuspendOrActivateProcessInstance() {
        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3.查询流程定义对象 获取流程定义查询对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("_100").singleResult();
        //4.判断流程定义是否挂起
        boolean suspended = processDefinition.isSuspended();
        //5.根据流程定义id
        String id = processDefinition.getId();
        //6.判断流程定义是否挂起 如果挂起则激活
        if (suspended) {
            //如果挂起则激活,参数1：流程定义id，参数2：是否激活流程实例，参数3：时间
            repositoryService.activateProcessDefinitionById(id, true, null);
            System.out.println("流程定义：" + id + " 激活成功");
        }else{
            //如果激活则挂起
            repositoryService.suspendProcessDefinitionById(id, true, null);
            System.out.println( "流程定义：" + id + " 挂起成功");
        }

    }
    /**
     * 单个流程示例挂起 /激活
     */
    @Test
    public void testSuspendOrActiveProcessInstanceById() {
        //1.获取流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RuntimeService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.根据流程定义id查询流程定义对象
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("22501").singleResult();
        //4.判断流程定义是否挂起
        boolean suspended = processInstance.isSuspended();
        //5.根据流程定义id
        String id = processInstance.getId();
        //6.判断流程定义是否挂起 如果挂起则激活
        if (suspended) {
            //如果挂起则激活,参数1：流程定义id，参数2：是否激活流程实例，参数3：时间
            runtimeService.activateProcessInstanceById(id);
            System.out.println("流程定义：" + id + " 激活成功");
        }else{
            //如果激活则挂起
            runtimeService.suspendProcessInstanceById(id);
            System.out.println( "流程定义：" + id + " 挂起成功");

        }
    }
    /**
     * 完成我的任务
     */
    @Test
    public void testCompleteMyPersonalTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //只返回一个值
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("_100").taskAssignee("worker").list();
        for (Task task : list) {
            taskService.complete( task.getId());
            System.out.println( "任务ID：" + task.getId());
            System.out.println( "任务名称：" + task.getName());
            System.out.println( "任务创建时间：" + task.getCreateTime());
            System.out.println( "任务办理人：" + task.getAssignee());
            System.out.println( "流程实例ID：" + task.getProcessInstanceId());
            System.out.println( "##############################");
        }
        //任务ID

        System.out.println( "完成任务");
    }
}
